﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.Entity;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class EditPriceScheme : BasePage
    {
        private IPolicySchemeBLL policySchemeBLL = IocContainer.Instance.Resolve<IPolicySchemeBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.txtStartDate.Attributes.Add("readonly", "true");
                this.txtEndDate.Attributes.Add("readonly", "true");
                if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int id = 0;
                    int.TryParse(Request.QueryString["Id"], out id);
                    if (id > 0)
                    {
                        PolicyScheme scheme = policySchemeBLL.GetPolicyById(id);
                        this.txtSchemeName.Text = scheme.SchemeName;
                        this.txtSchemeCode.Text = scheme.SchemeCode;
                        this.ddlPriceType.SelectedValue =scheme.PriceType;
                        this.txtPrice.Text = scheme.Price.ToString();
                        this.txtTaxPrice.Text = scheme.TaxPrice.ToString();
                        this.txtUnit.Text = scheme.Unit;
                        this.txtStartDate.Text =scheme.StartTime!=null? scheme.StartTime.Value.ToString("yyyy-MM-dd"):"";
                        this.txtEndDate.Text =scheme.EndTime!=null? scheme.EndTime.Value.ToString("yyyy-MM-dd"):"";
                        this.ddlSpecialType.SelectedValue =(Convert.ToInt16(scheme.IsSpecialType)).ToString();
                        this.txtProductId.Value = scheme.ProductId.ToString();
                        this.txtProductName.Text = scheme.ProductName;
                        this.txtTaxRatio.Text = scheme.TaxRatio.ToString();
                        this.txtTaxPrice.Attributes.Add("readonly","readonly");

                    }
                }
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Boolean isOK = false;
                int policyId = 0;
                int.TryParse(Request.QueryString["PolicyId"], out policyId);
                PolicyScheme scheme = new PolicyScheme();
                scheme.SchemeName =txtSchemeName.Text.Trim();
                scheme.SchemeCode =txtSchemeCode.Text.Trim();
                scheme.PolicyId = policyId;
                scheme.PriceType =this.ddlPriceType.SelectedValue;
                scheme.Price =Convert.ToDecimal(txtPrice.Text.Trim());
                scheme.TaxPrice = Convert.ToDecimal(txtTaxPrice.Text.Trim());
                scheme.Unit = this.txtUnit.Text.Trim();
                scheme.IsSpecialType =this.ddlSpecialType.SelectedValue=="0"?false:true;
                scheme.StartTime = Convert.ToDateTime(this.txtStartDate.Text.Trim());
                scheme.EndTime = Convert.ToDateTime(this.txtEndDate.Text);
                scheme.ProductId = Convert.ToInt32(this.txtProductId.Value);
                scheme.TaxRatio = Convert.ToDecimal(this.txtTaxRatio.Text);
               
                if ("add" == Request.QueryString["Type"].ToString().ToLower())
                {
                    policySchemeBLL.Add(scheme);
                    isOK = true;
                }
                else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                {
                    int id = 0;
                    int.TryParse(Request.QueryString["Id"], out id);
                    scheme.Id = id;
                    policySchemeBLL.Update(scheme);
                    isOK = true;
                }

                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }

        }
    }
}
